/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package graphics;

import java.util.Arrays;
import javax.swing.JOptionPane;
import tools.MessageManager;
import tools.SecureConnector;
import tools.TimeManager;
import user.Statistic;

/**
 *
 * @author Mattia
 */
public class Login extends javax.swing.JFrame {

    MainFrame x;
    private boolean memCred;

    /**
     * Creates new form Login
     */
    public Login(MainFrame x) {
        this.x = x;
        Statistic stat = new Statistic();
        stat.readCredFile();
        memCred = stat.isMemCred();
        initComponents();
        if (memCred) {
            this.Username_Field.setText(stat.getUsername());
            this.Password_Field.setText(stat.getPassword());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        MainPanel = new javax.swing.JLayeredPane();
        Label_Title = new javax.swing.JLabel();
        Username_Label = new javax.swing.JLabel();
        Username_Field = new javax.swing.JTextField();
        Password_Field = new javax.swing.JPasswordField();
        Password_Label = new javax.swing.JLabel();
        MemorizzaCredenziali = new javax.swing.JCheckBox();
        Info = new javax.swing.JTextPane();
        BTN_Login = new javax.swing.JButton();
        Logo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setUndecorated(true);
        setResizable(false);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                KeyListner(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                KeyListner(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                KeyListner(evt);
            }
        });

        MainPanel.setBackground(new java.awt.Color(255, 255, 255));
        MainPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        MainPanel.setOpaque(true);
        MainPanel.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                KeyListner(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                KeyListner(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                KeyListner(evt);
            }
        });

        Label_Title.setFont(new java.awt.Font("Honest John's", 0, 48)); // NOI18N
        Label_Title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Label_Title.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/image/Login_Logo.png"))); // NOI18N
        Label_Title.setBounds(10, 10, 270, 80);
        MainPanel.add(Label_Title, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Username_Label.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        Username_Label.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        Username_Label.setText("Username");
        Username_Label.setBounds(10, 70, 80, 30);
        MainPanel.add(Username_Label, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Username_Field.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                KeyListner(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                KeyListner(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                KeyListner(evt);
            }
        });
        Username_Field.setBounds(100, 70, 180, 30);
        MainPanel.add(Username_Field, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Password_Field.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                KeyListner(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                KeyListner(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                KeyListner(evt);
            }
        });
        Password_Field.setBounds(100, 110, 180, 30);
        MainPanel.add(Password_Field, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Password_Label.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        Password_Label.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        Password_Label.setText("Password");
        Password_Label.setBounds(10, 110, 80, 30);
        MainPanel.add(Password_Label, javax.swing.JLayeredPane.DEFAULT_LAYER);

        MemorizzaCredenziali.setBackground(new java.awt.Color(255, 255, 255));
        MemorizzaCredenziali.setSelected(this.memCred);
        MemorizzaCredenziali.setText("Memorizza Credenziali");
        MemorizzaCredenziali.setOpaque(false);
        MemorizzaCredenziali.setBounds(10, 143, 270, 20);
        MainPanel.add(MemorizzaCredenziali, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Info.setEditable(false);
        Info.setBorder(null);
        Info.setFont(new java.awt.Font("Tahoma", 2, 9)); // NOI18N
        Info.setText("Per informazioni circa la registrazione e l'utilizzo dei dati personali visita amunaptra.zagomattia.it");
        Info.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        Info.setEnabled(false);
        Info.setOpaque(false);
        Info.setSelectionColor(new java.awt.Color(255, 255, 255));
        Info.setBounds(10, 170, 130, 70);
        MainPanel.add(Info, javax.swing.JLayeredPane.DEFAULT_LAYER);

        BTN_Login.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        BTN_Login.setText("Login");
        BTN_Login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTN_LoginActionPerformed(evt);
            }
        });
        BTN_Login.setBounds(200, 210, 80, 21);
        MainPanel.add(BTN_Login, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Logo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/image/Logo_v3_small.png"))); // NOI18N
        Logo.setVerifyInputWhenFocusTarget(false);
        Logo.setBounds(-10, 100, 300, 140);
        MainPanel.add(Logo, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(MainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(MainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BTN_LoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTN_LoginActionPerformed
        login();
    }//GEN-LAST:event_BTN_LoginActionPerformed

    private boolean checkLogin() {
        //String user = "Debug";
        //char[] psw = {'m','a','t','t','i','a'};
        String cause = "bad username";
        SecureConnector SConn = new SecureConnector(this.Username_Field.getText(), new String(this.Password_Field.getPassword()));
        if (SConn.isSuccess()) {
            if (this.MemorizzaCredenziali.isSelected()) {
                Statistic stat = new Statistic();
                stat.setMemCred(true);
                stat.setUsername(this.Username_Field.getText());
                stat.setPassword(this.Password_Field.getPassword());
                stat.updateCredFile();
            } else {
                if (memCred) {
                    new Statistic().reset();
                }
            }
            Statistic.updateStatFile(TimeManager.getTimeToString() + " - " + System.getProperty("user.name") + " logged in as \"" + this.Username_Field.getText() + "\"");
            return true;
            } else {
            cause = "bad password";
        
        Statistic.updateStatFile(TimeManager.getTimeToString() + " - " + System.getProperty("user.name") + " attempt to login in as \"" + this.Username_Field.getText() + "\" - " + cause);
        return false;
        }
    }

    private void login() {
        if (checkLogin()) {
            dispose();
            x.setLocationRelativeTo(null);
            x.setVisible(true);
        } else {
            MessageManager.generic(this, "Username e/o password errati!", "Login Errato", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void KeyListner(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_KeyListner

        switch (evt.getKeyCode()) {
            case java.awt.event.KeyEvent.VK_ENTER:
                login();
                break;
            case java.awt.event.KeyEvent.VK_ESCAPE:
                System.exit(0);
                break;
        }
    }//GEN-LAST:event_KeyListner
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BTN_Login;
    private javax.swing.JTextPane Info;
    private javax.swing.JLabel Label_Title;
    private javax.swing.JLabel Logo;
    private javax.swing.JLayeredPane MainPanel;
    private javax.swing.JCheckBox MemorizzaCredenziali;
    private javax.swing.JPasswordField Password_Field;
    private javax.swing.JLabel Password_Label;
    private javax.swing.JTextField Username_Field;
    private javax.swing.JLabel Username_Label;
    // End of variables declaration//GEN-END:variables
}
